package com.itlong.cloud.operate;

import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <desc>
 * 刷数据
 * <desc/>
 *
 * @createDate 2021/04/27
 */
@EnableScheduling
@RestController
public class OperateData {

    private static Logger LOG = LoggerFactory.getLogger(OperateData.class);

    @Autowired
    IOperateProjectServiceClient iOperateProjectServiceClient;

    public String format(Date d) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
    }

    public String getStartPoint(Calendar c) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        c.set(year, month, day, 0, 0, 0);
        return format(c.getTime());
    }

    public String getEndPoint(Calendar c) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        c.set(year, month, day, 23, 59, 59);
        return format(c.getTime());
    }

    /**
     * <desc>
     * 把通行记录最近7天的数据存到另一个表
     * 每天的3点处理前一天的数据
     * <desc/>
     *
     * @author zhouchun
     * @createDate 2021/04/27
     */
    @Scheduled(cron = "0 0 3 * * *")
    public void timeTaskDevicePassRecordLatestSave() {
        Calendar s1 = Calendar.getInstance();
        Calendar e1 = Calendar.getInstance();
        Calendar s2 = Calendar.getInstance();
        Calendar e2 = Calendar.getInstance();
        s1.add(Calendar.DATE, -180);
        e1.add(Calendar.DATE, -8);
        s2.add(Calendar.DATE, -1);
        e2.add(Calendar.DATE, -1);
        String startTime1 = getStartPoint(s1);
        String endTime1 = getEndPoint(e1);
        String startTime2 = getStartPoint(s2);
        String endTime2 = getEndPoint(e2);
        //第1对时间是删除数据的时间
        //第2对时间是插入数据的时间
        LOG.info("【OperateData】{} {} {} {}", startTime1, endTime1, startTime2, endTime2);
        iOperateProjectServiceClient.timeTaskDevicePassRecordLatestSave(startTime1, endTime1, startTime2, endTime2);
    }

}